<template>
  <div class="app-container">

    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="22" :xs="24">
        <!-- 搜索框，根据条件查询    -->
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="70px">

          <el-form-item label="项目名称" prop="srName">
            <el-input
              v-model="queryParams.srName"
              placeholder="请输入项目名称"
              clearable
              size="small"
              style="width: 160px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="可用状态"
              clearable
              size="small"
              style="width: 160px"
            >
              <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>

          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>

        </el-form>
        <!--  增删改查按钮    -->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:rexpense:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:rexpense:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:rexpense:remove']"
            >删除</el-button>
          </el-col>
        </el-row>
        <!-- 查出来的数据表格展示 也包含每条数据后面修改删除分配角色按钮  -->
        <el-table v-loading="loading" :data="rexpenseList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="挂号费用Id" align="center" key="srId" prop="srId" width="100"/>
          <el-table-column label="挂号费名称" align="center" key="srName" prop="srName" :show-overflow-tooltip="true" />
          <el-table-column label="挂号费" align="center" key="srPrice" prop="srPrice" :show-overflow-tooltip="true" />
          <el-table-column label="状态"align="center" key="status" prop="status"  :formatter="statusFormat" ></el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" width="150">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="200"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:rexpense:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:rexpense:remove']"
              >删除</el-button>
            </template>
          </el-table-column>

        </el-table>
        <!--分页展示相关  -->
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
          <el-form-item label="挂号费名称"  prop="srName">
            <el-input v-model="form.srName" placeholder="请输入挂号费名称" maxlength="11" />
          </el-form-item>
        </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="金额" prop="srPrice">
              <el-input-number v-model="form.srPrice" placeholder="0.00"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
          <el-form-item label="状态">
            <el-radio-group v-model="form.status">
              <el-radio
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{dict.dictLabel}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>




<script>
  // 导入，需要发请求的方法都要在这导入相应的方法所在的js文件
import { listRexpense, addRexpense,getReById,updateRexpense,delRexpense} from "@/api/system/rexpense";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "rexpense",
  components: { Treeselect },

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      rexpenseList: null,
      // 弹出层标题
      title: "",
      // 是否显示增加修改弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 状态数据字典
      statusOptions: [],
       //用户数据集合
      rexpense:[],
      // 表单参数
      form: {},

      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        srId:undefined,
        srName:undefined,
        status: undefined,
      },
      // 表单校验
      rules: {
          srName: [
            { required: true, message: "挂号费名称不能为空", trigger: "blur" }
          ],
      }
    };
  },

  created() { //已创建，相当于onload事件，即页面加载时就会执行的方法，而且当一个方法执行成功后，下面的方法不会执行。
    this.getList();
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
     /** 分页查询用户列表 */
      getList() {
      this.loading = true;
      listRexpense(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.rexpenseList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

  //  对话框取消时关闭对话框。
    cancel() {
        this.open = false;
        this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
          srId: undefined,
          srName:undefined,
          srPrice:undefined,
          status: "0",
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 用户多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.srId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

      // 菜单状态字典翻译1状态
      statusFormat(row, column) {
          return this.selectDictLabel(this.statusOptions, row.status);
      },
      /** 新增按钮操作 */
      handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目信息";
    },

    /** 修改按钮操作 */
      handleUpdate(row) {
          this.reset();
          const srId = row.srId|| this.ids;
          getReById(srId).then(response => {
              this.form = response.data;
              this.open = true;
              this.title = "修改用户";
          });
      },

      /** 新增修改提交按钮 */
      submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.srId != undefined) {
              updateRexpense(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRexpense(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },


    /** 删除按钮操作 */
    handleDelete(row) {
      const srIds = row.srId || this.ids;
      this.$confirm('是否确认删除用户ID为"' + srIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRexpense(srIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
  }
};
</script>
